package ink.lovejinhu.special.dp;

/**
 * @author jinhu
 * created 2021-07-26 14:43
 */
public class Solution_0005 {
    /**
     * 最长回文字串
     *
     * @param s
     * @return
     */
    public String longestPalindrome(String s) {

        int max = 0;
        int left = 0;
        int right = 0;
        for (int i = 0; i < s.length(); i++) {

            for (int j = i + max - 1; j < s.length(); j++) {
                if (this.ishuiwen(s, i, j)) {
                    if ((j - i + 1) > max) {
                        max = j - i + 1;
                        left = i;
                        right = j;
                    }
                }

            }

        }
        StringBuilder res = new StringBuilder();
        for (int i = left; i <= right; i++) {
            res.append(s.charAt(i));
        }
        return res.toString();
    }

    public boolean ishuiwen(String s, int i, int j) {
        for (; i < j; i++, j--) {
            if (!(s.charAt(i) == s.charAt(j)))
                return false;

        }
        return true;
    }
}
